package com.xiaoyu.lanling.feature.guard.viewholder

import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.facebook.drawee.view.SimpleDraweeView
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.guard.datamodel.BaseGuardItem
import com.xiaoyu.lanling.feature.guard.datamodel.GuardItem
import com.xiaoyu.lanling.media.image.ImageLoadParam
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.view.UserAvatarDraweeView
import com.xiaoyu.lanling.view.text.UserNameTextView

abstract class BaseGuardViewHolder<T : BaseGuardItem> : ViewHolderBase<T>() {

    protected abstract val onClickListener: View.OnClickListener

    private lateinit var avatar: UserAvatarDraweeView
    private lateinit var name: UserNameTextView
    private lateinit var daysDesc: TextView
    private lateinit var levelIcon: SimpleDraweeView

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.guard_list_item, parent, false)
        avatar = view.findViewById(R.id.avatar)
        name = view.findViewById(R.id.name)
        daysDesc = view.findViewById(R.id.days_desc)
        levelIcon = view.findViewById(R.id.icon)

        view.setOnClickDebounceListener(onClickListener)
        return view
    }

    override fun showData(position: Int, itemData: T) {
        ImageViewUtil.loadImage(avatar, itemData.user, 56)
        name.setUser(itemData.user)
        daysDesc.text = AppContext.getString(R.string.guard_item_days_desc, itemData.days)
        ImageViewUtil.loadImage(levelIcon, ImageLoadParam.newBuilder().setEnableEmptyUrl(true).setUrl(itemData.levelIcon).setWidthInDP(80).setHeightInDP(24).build())
        mCurrentView.setViewTag(itemData)
    }
}